home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / shell / csh548src.lha / main.c < prev    next >
C/C++ Source or Header  |  1995-09-16  |  19KB  |  772 lines

  1. /*
  2.  * MAIN.C
  3.  *
  4.  * Matthew Dillon, 24 Feb 1986
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  9.  * Version 5.00L by U. Dominik Mueller 17-Feb-91
  10.  * Version 5.20L by Andreas M. Kirchwitz (Fri, 13 Mar 1992)
  11.  *
  12.  */
  13.  
  14. #include "shell.h"
  15.  
  16.  
  17. #define CSH_VER "5"
  18. #define CSH_REV "48"
  19.  
  20.  
  21. static struct Window *getwindow(void);
  22. static void exectimer(int stop);
  23. static void set_kickversion(void);
  24. char shellcompiled[]="Compiled: "__DATE__" "__TIME__" with "COMPILER"\n";
  25.  
  26. #if 1
  27. char shellname[]    ="Csh "CSH_VER"."CSH_REV" (public release)";
  28. #else
  29. char shellname[]    ="Csh "CSH_VER"."CSH_REV" (BETA)";
  30. #endif
  31.  
  32. char shellversion[] =CSH_VER""CSH_REV;
  33. char shellvers[]    =CSH_VER"."CSH_REV;
  34. char shellv   []    ="\0$VER: csh "CSH_VER"."CSH_REV" "__AMIGADATE__"";
  35. char shellctr []    ="CshCounter";
  36. char shellres []    ="CshResident";
  37. char shellthere[]   ="CshLoggedIn";
  38.  
  39. char *oldtitle = NULL;
  40. char trueprompt[100];
  41. char Inline[260];
  42. extern struct ExecBase *SysBase;    /* standard fare....*/
  43. extern struct DosLibrary *DOSBase;    /* more standard fare.... */
  44. struct IntuitionBase *IntuitionBase;
  45. struct GfxBase *GfxBase;
  46. struct Library *GadToolsBase;
  47. struct Library *AslBase;        /* AMK: Asl-FileRequester replaces ARP */
  48. struct Library *BattClockBase;
  49.  
  50. struct Window *old_WindowPtr = NULL;
  51. int    oldtaskpri = -999;
  52. BPTR   OldCin;
  53. void   *PatternBase;
  54. BOOL   nologout = FALSE;
  55. BOOL   nowintitle = FALSE;
  56. struct MsgPort *acs_oldport = NULL;    /* old MsgPort */
  57.  
  58. #ifdef DO_ACS_KLUDGE
  59. /*
  60.  * - New option "-K" (Kludge) for KingCON/ToolManager, sends DOS-Paket
  61.  *    ACTION_CHANGE_SIGNAL.  Don't use this option if you have no problems.
  62.  */
  63. int    acs_kludge = 0;            /* ACTION_CHANGE_SIGNAL kludge */
  64. #endif
  65.  
  66. #ifdef MULTIUSER_SUPPORT
  67. struct muBase *muBase = NULL;     /* LILJA: Added for multiuser-support */
  68. #endif
  69.  
  70. static char *defset[]={
  71.     v_histnum,  "0",
  72.     v_titlebar, shellname,
  73.     v_hist,     "50",
  74.     v_lasterr,  "0",
  75.     v_stat,     "0",
  76.     v_path,     "RAM:,RAM:c,df0:c,df1:c,sys:system,csh:,s:",
  77.     v_rxpath,   "REXX:",
  78.     v_scroll,   "2",
  79.     v_minrows,  "34",
  80.     v_hilite,   "c7",
  81.     v_lcd,      "",
  82.     v_qcd,      "csh:csh-qcd",
  83.     v_insert,   "1",
  84.     v_abbrev,   "5",
  85.     "_terminal","",
  86.     "_man",     "csh:csh.doc",
  87.     "_version", shellversion,
  88. /*    v_nomatch,  "1",*/
  89.     v_prghash,  "csh:csh-prgs",
  90.     v_complete, "*",
  91.     NULL,       NULL
  92. };
  93.  
  94. static char *defalias[]={
  95.     "cls",  "echo -n ^l",
  96.     "dswap","cd $_lcd",
  97.     "exit", "endcli;quit",
  98.     "cdir", "%q cd $q; cls; dir",
  99.     "q",    "quit",
  100.     "rx",   "%q RX $q",
  101.     "manlist", "search -nl $_man \"    \"",
  102.     NULL,   NULL
  103. };
  104.  
  105. struct MsgPort *Console=(struct MsgPort *)-1;
  106. long ExecTimer, ExecRC;
  107.  
  108. #ifdef __SASC
  109. long __stack = 17500L;
  110. #endif
  111.  
  112. main(int argc, char *argv[])
  113. {
  114.     static ROOT locals;
  115.     BPTR fh;
  116.     int i;
  117.     char buf[10];
  118.     BOOL login=FALSE,nologin=FALSE,nocshrc=FALSE,noclrmenu=FALSE;
  119. #ifdef AZTEC_C
  120.     extern int Enable_Abort;
  121.     Enable_Abort = 0;
  122. #endif
  123.  
  124.     MyMem=salloc(4);
  125.  
  126.     if( argc==0 ) {              /* Prevent starting from workbench */
  127.         Delay(60);
  128.         exit(0);
  129.     }
  130.  
  131.     Myprocess = (struct Process *)FindTask(NULL);
  132.     OldCin    = Myprocess->pr_CIS;
  133.     Mycli     = Cli();
  134. /*    Mycli     = (struct CommandLineInterface *)((long)Myprocess->pr_CLI << 2);*/
  135.  
  136.     init_mbase();
  137.     push_locals( &locals );
  138.     initmap();
  139.  
  140.     if (!SysBase || SysBase->LibNode.lib_Version<37) {
  141.         printf("Sorry, you'll need at least V37 to run CSH.\n");
  142.         exit(20);
  143.     }
  144.  
  145.     if (!DOSBase) {
  146.         printf("No dos library\n");
  147.         exit(20);
  148.     }
  149.  
  150.     /* we do not work without it ... */
  151.     DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  152.  
  153.     if (!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))) {
  154.         printf("No graphics library\n");
  155.         exit(20);
  156.     }
  157.  
  158.     if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))) {
  159.         printf("No intuition library\n");
  160.         CloseLibrary((struct Library *)GfxBase);
  161.         exit(20);
  162.     }
  163.  
  164.     if (!(AslBase=OpenLibrary("asl.library",37L))) {
  165.         printf("No asl library\n");
  166.         CloseLibrary((struct Library *)IntuitionBase);
  167.         CloseLibrary((struct Library *)GfxBase);
  168.         exit(20);
  169.     }
  170.  
  171.     if (!(GadToolsBase=OpenLibrary("gadtools.library",37L))) {
  172.         printf("No gadtools library\n");
  173.         CloseLibrary((struct Library *)AslBase);
  174.         CloseLibrary((struct Library *)IntuitionBase);
  175.         CloseLibrary((struct Library *)GfxBase);
  176.         exit(20);
  177.     }
  178.  
  179. #ifdef MULTIUSER_SUPPORT
  180.     /* LILJA: Added for multiuser-support */
  181.     muBase = (struct muBase *)OpenLibrary("multiuser.library",39);
  182. #endif
  183.  
  184.     PatternBase = NULL; /* OpenLibrary("pattern.library",5L); */
  185.     BattClockBase = OpenResource(BATTCLOCKNAME);
  186.  
  187.     set_var(LEVEL_SET,"_kick2x",(SysBase->LibNode.lib_Version>=37)?"1":"0");
  188.     set_var(LEVEL_SET,"_kick3x",(SysBase->LibNode.lib_Version>=39)?"1":"0");
  189.  
  190.     set_kickversion();
  191.  
  192.     if( !IsInteractive(Input())) {
  193.         o_bground=1;
  194.         Mycli->cli_Background=DOSTRUE;
  195.     } else
  196.         Mycli->cli_Background=DOSFALSE;
  197.  
  198.     set_var( LEVEL_SET, v_bground, (o_bground) ? "1" : "0" );
  199.  
  200.     /* We don't really need "CONSOLE:", maybe good for stderr ... */
  201.     if( fh=Open( "CONSOLE:" , MODE_NEWFILE) ) {
  202.         Console= ((struct FileHandle *)(4*fh))->fh_Type;
  203.         Close(fh);
  204.     }
  205.  
  206.     Forbid();
  207.     i=0;
  208.     /* AMK: OS20-GetVar replaces ARP-Getenv, SetVar replaces Setenv */
  209.     if ( GetVar(shellthere,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) <  0L) {
  210.         login=TRUE;
  211.         SetVar(shellthere,"1",-1L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  212.     }
  213.     if ( GetVar(shellres  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  214.         o_resident=1;
  215.     if ( GetVar(shellctr  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  216.         i=atoi(buf);
  217.     sprintf(buf, "%d", i+1);
  218.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  219.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  220.     Permit();
  221.  
  222. #ifdef AZTEC_C
  223.     stdin->_flags    |= _IONBF;    /* make sure we're set as an unbuffered tty */
  224.     stdout->_flags    |= _IONBF;    /* in case of redirection in .login */
  225.     Close( (BPTR)_devtab[2].fd);
  226.     _devtab[2].mode |= O_STDIO;
  227.     _devtab[2].fd = _devtab[1].fd;    /* set stderr to Output() otherwise */
  228.                     /* don't work with aux driver */
  229. #else
  230.     /* if( setvbuf( stdout,NULL,_IOLBF,BUFSIZ )) exit(20); */
  231.     /* setnbf( stdout ); */
  232.     /* Close( _ufbs[2] );*/
  233.     /*_ufbs[2]=_ufbs[1]; */
  234.     /* setnbf( stderr ); */
  235. #endif
  236.  
  237.     sprintf(buf,"%ld",Myprocess->pr_TaskNum);
  238.     set_var(LEVEL_SET, "_clinumber", buf);
  239.  
  240.     seterr(0);
  241.     if (Myprocess->pr_CurrentDir == NULL)
  242.         execute("cd :");
  243.     set_cwd();
  244.  
  245.     o_nowindow= 1;
  246.  
  247.     set_var(LEVEL_SET,v_prompt, (IsInteractive(Input())) ? "%c%p> ":"");
  248.  
  249.     for( i=0; defset[i]; i+=2 )
  250.         set_var( LEVEL_SET, defset[i], defset[i+1] );
  251.     for( i=0; defalias[i]; i+=2 )
  252.         set_var( LEVEL_ALIAS, defalias[i], defalias[i+1] );
  253.  
  254.     o_nowindow= 0;
  255.  
  256.     for (i = 1; i < argc; ++i) {
  257.         if (*argv[i]=='-' && (index(argv[i],'a') || index(argv[i],'t')))
  258.             o_nowindow=1;
  259.         if (*argv[i]=='-' && index(argv[i],'n'))
  260.             nologin = TRUE;
  261.         if (*argv[i]=='-' && index(argv[i],'N'))
  262.             nocshrc = TRUE;
  263.         if (*argv[i]=='-' && index(argv[i],'L'))
  264.             nologout = TRUE;
  265.         if (*argv[i]=='-' && index(argv[i],'M'))
  266.             noclrmenu = TRUE;
  267.         if (*argv[i]=='-' && index(argv[i],'W'))
  268.             nowintitle = TRUE;
  269.         if (*argv[i]=='-' && index(argv[i],'R'))
  270.             o_noraw = 1;
  271.         if (*argv[i]=='-' && index(argv[i],'w'))
  272.             o_nowindow = 1;
  273.         if (*argv[i]=='-' && (index(argv[i],'C') || index(argv[i],'c'))) {
  274.             noclrmenu  = TRUE;
  275.             nowintitle = TRUE;
  276.         }
  277.     }
  278.  
  279.     if (Mycli->cli_Background) {
  280.         o_noraw    = 1;
  281.         o_nowindow = 1;
  282.         o_vt100    = 1;
  283.     }
  284.  
  285.     if( !o_nowindow && (Mywindow=getwindow()) && IsInteractive(Input())) {
  286.         old_WindowPtr = Myprocess->pr_WindowPtr;
  287.         Myprocess->pr_WindowPtr = 0L/*Mywindow*/;
  288.         newwidth();
  289.         if (!nowintitle)
  290.             oldtitle=(char *)(Mywindow->Title);
  291.         if (!noclrmenu) {
  292.             /* clear menus, even if we are not the owner */
  293.             set_menu();
  294.         }
  295.     }
  296.  
  297.     if (login && !nologin) {
  298.         /*printf("we're the first csh today, mon ami!\n");*/
  299.         if( exists("S:.login"))
  300.             execute("source S:.login");
  301.     }
  302.  
  303.     if (!nocshrc) {
  304.         if( exists("S:.cshrc"))
  305.             execute("source S:.cshrc");
  306.     }
  307.  
  308.     {
  309.     char nam1[40],nam2[40],nam3[40];
  310.     BOOL e1,e2,e3;
  311.     strcpy(nam1,"ENVARC:"); strcat(nam1,shellctr);   e1=exists(nam1);
  312.     strcpy(nam2,"ENVARC:"); strcat(nam2,shellres);   e2=exists(nam2);
  313.     strcpy(nam3,"ENVARC:"); strcat(nam3,shellthere); e3=exists(nam3);
  314.  
  315.     if (e1 || e2 || e3) {
  316.         printf("\nWARNING: please remove the following files from ENVARC:\n\n");
  317.         if (e1)
  318.         printf("          - %s\n",shellctr);
  319.         if (e2)
  320.         printf("          - %s\n",shellres);
  321.         if (e3)
  322.         printf("          - %s\n",shellthere);
  323.         printf("\n         and reboot your system after removing the files.\n");
  324.         printf("         Never copy ENV: to ENVARC: !!\n\n");
  325.         Delay(150L);
  326.     }
  327.     }
  328.  
  329.     /* would be a nice idea to have a flag that tells if this is
  330.        a full shell or just executing some commands and the leaving */
  331.  
  332.     for (i = 1; i < argc; ++i) {
  333.         if (*argv[i]=='-') {
  334.             char *str=argv[i]+1;
  335.             for( ; *str; str++ ) {
  336.                 switch( *str) {
  337.                 case 'c':
  338.                 case 'C': execute( compile_av( argv, i+1, argc, ' ',*str=='C'));
  339.                           main_exit(Lastresult); break;
  340.                 case 'k': set_var(LEVEL_SET,v_nobreak,"1"); break;
  341. #ifdef DO_ACS_KLUDGE
  342.                 case 'K': acs_kludge = 1; break;
  343. #endif
  344.                 case 'v': Verbose=1; set_var(LEVEL_SET,v_verbose,"hs"); break;
  345.                 case 'b': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  346.                           SetTaskPri( &Myprocess->pr_Task, -1 ); break;
  347.                 case 'f': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  348.                           SetTaskPri( &Myprocess->pr_Task,  1 ); break;
  349.                 case 'a': o_nowindow= o_noraw= 1;
  350.                           set_var( LEVEL_SET, v_hilite, "" ); break;
  351. #if 0
  352.                 case 'm': unset_var(LEVEL_SET,v_nomatch); break;
  353. #endif
  354.                 case 'm': set_var(LEVEL_SET,v_nomatch,"1"); break;
  355.                 case 't': o_nowindow= o_vt100= o_nofastscr= 1;
  356.                           set_var( LEVEL_SET, v_hilite, "b" /*"r"*/ );
  357.                           set_var( LEVEL_SET, v_noreq, "1" );
  358.                           set_var( LEVEL_SET, "_terminal", "1" );
  359.                           set_var( LEVEL_ALIAS, "cls", "e -n ^[[0\\;0H^[[J" );
  360.                           break;
  361.                 case 's': DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  362.                           break;
  363.                 case 'w': /* just don't use window pointer */
  364.                           o_nowindow = 1;
  365.                           set_var( LEVEL_SET, "_timeout", "1" ); /* GWB_TIMEOUT_LOCAL */
  366.                           break;
  367.                 case 'V': /* strict VT100 mode */
  368.                           o_vt100 = 1;
  369.                           break;
  370.                 }
  371.             }
  372.         } else {
  373.             sprintf (Inline, "source %s",argv[i]);
  374.             execute (Inline);
  375.         }
  376.     }
  377.  
  378.     /* we should do "main_exit(Lastresult);" here if
  379.        csh was called with some files to source ... */
  380.  
  381. #if 0
  382.     if( fh=Open( "CONSOLE:" , MODE_NEWFILE) ) {
  383.         if (Console != ((struct FileHandle *)(4*fh))->fh_Type)
  384.             Write(fh,"Funny Console!\n",15);
  385.         else
  386.             Write(fh,"Real Console!\n",14);
  387.         Close(fh);
  388.     }
  389.     else
  390.         printf("No Console!\n");
  391.  
  392.     Write(Input(),"Input()!\n",9);
  393.     Write(Output(),"Output()!\n",10);
  394. #endif
  395.  
  396. #if 0
  397.     /* cannot exit because of "echo mem | csh" or "csh <cmd_file */
  398.  
  399.     if( !IsInteractive(Input()) ) {
  400.         main_exit( Lastresult );
  401.     }
  402. #endif
  403.  
  404. #ifdef DO_ACS_KLUDGE
  405.     if (acs_kludge>0) {
  406.         BPTR bfh = Open("*", MODE_OLDFILE);
  407.         struct FileHandle *fh;
  408.         long xrslt,xioerr;
  409.         if (bfh != NULL) {
  410.             fh = BADDR(bfh);
  411.             if (fh->fh_Type != NULL) {
  412.                 xrslt = DoPkt( (void *)fh->fh_Type, ACTION_CHANGE_SIGNAL, fh->fh_Arg1, (long)&(Myprocess->pr_MsgPort), NULL,NULL,NULL);
  413.                 /* also possible instead of fh->fh_Type: Myprocess->pr_ConsoleTask */
  414.                 xioerr = IoErr();
  415.                 acs_oldport = (struct MsgPort *)xioerr;
  416.                 printf("ACS_1: DoPkt()=%ld, fh_Type=%ld, ConTask=%ld, pr_MsgPort=%ld, IoErr()=%ld\n",xrslt,(long)fh->fh_Type,(long)Myprocess->pr_ConsoleTask,(long)&(Myprocess->pr_MsgPort),xioerr);
  417.             }
  418.             Close(bfh);
  419.         }
  420.         acs_kludge = 2;
  421.     }
  422. #endif
  423.  
  424.     for (;;) {
  425.         if (breakcheck())
  426.             while (WaitForChar(Input(), 100L) || CHARSWAIT( stdin ))
  427.                 gets(Inline);
  428.         clearerr(stdin);  /* prevent acidental quit */
  429.         /*
  430.            AMK: new position of breakreset() before exec_every(),
  431.                 old position caused enforcer hits
  432.                 (do 'set _every "set cols = $(wincols)"' and
  433.                  then abort (^c) a command)
  434.         */
  435.         breakreset();
  436.         exec_every();
  437.  
  438.         { /* AMK TEST BEGIN */
  439.             char *old;
  440.             char pwd[256];
  441.  
  442.             /* no requesters when no disk is inserted */
  443.             Myprocess->pr_WindowPtr = (APTR)(-1);
  444.  
  445. #if 1
  446.             if (NameFromLock(Myprocess->pr_CurrentDir, pwd, 255L)) {
  447.                 if( !(old=get_var(LEVEL_SET, v_cwd)) )
  448.                     old="";
  449.                 if (strcmp(pwd,old)) {
  450.                     set_var(LEVEL_SET, v_lcd, old);
  451.                     set_cwd();
  452.                 }
  453.             }
  454. #else
  455.             if (!NameFromLock(Myprocess->pr_CurrentDir, pwd, 255L)) {
  456.                 fprintf(stderr,"csh.main: NameFromLock() failed\n");
  457.                 strcpy(pwd,"<unknown>");
  458.             }
  459.             if( !(old=get_var(LEVEL_SET, v_cwd)) )
  460.                 old="";
  461.             if (strcmp(pwd,old)) {
  462.                 set_var(LEVEL_SET, v_lcd, old);
  463.                 set_cwd();
  464.             }
  465. #endif
  466.             /*update_sys_var(v_cwd);*/
  467.  
  468.             Myprocess->pr_WindowPtr = o_noreq ? (APTR) -1L : 0L/*Mywindow*/;
  469.  
  470.         } /* AMK TEST BEGIN */
  471.  
  472.         update_sys_var(v_titlebar);
  473.         update_sys_var(v_prompt);
  474.         /*
  475.            AMK: old position of breakreset(), causes enforcer hits
  476.                 (do 'set _every "set cols = $(wincols)"' and
  477.                  then abort (^c) a command)
  478.         */
  479.         breakreset();
  480. #if RAW_CONSOLE
  481.         if (Quit || !rawgets(Inline, disable ? "_ " : trueprompt)) main_exit(0);
  482. #else
  483.         printf("%s", disable ? "_ " : trueprompt);
  484.         fflush(stdout);
  485.         if (Quit || !gets(Inline)) main_exit(0);
  486. #endif
  487.         breakreset();
  488.  
  489. #if 1
  490.         /* start/stop timer only if non-empty command line */
  491.  
  492.         if (*Inline && strlen(Inline)>0) {
  493.             exectimer(0);
  494.             ExecRC = exec_command(Inline);
  495.             exectimer(1);
  496.         }
  497. #else
  498.         /* start/stop timer for every command  */
  499.  
  500.         exectimer(0);
  501.         if (*Inline) ExecRC=exec_command(Inline);
  502.         exectimer(1);
  503. #endif
  504.     }
  505. }
  506.  
  507.  
  508. void
  509. main_exit(n)
  510. {
  511.     int i;
  512.     char buf[10];
  513.  
  514.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  515.     GetVar(shellctr,buf,10L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  516.     i=atoi(buf);
  517.     sprintf(buf,"%d",i-1);
  518.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  519.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  520.  
  521. #ifdef DO_ACS_KLUDGE
  522.     if (acs_kludge>0) {
  523.         BPTR bfh = Open("*", MODE_OLDFILE);
  524.         struct FileHandle *fh;
  525.         long xrslt,xioerr;
  526.         if (bfh != NULL) {
  527.             fh = BADDR(bfh);
  528.             if (fh->fh_Type != NULL) {
  529.                 xrslt = DoPkt( (void *)fh->fh_Type, ACTION_CHANGE_SIGNAL, fh->fh_Arg1, (long)acs_oldport, NULL,NULL,NULL);
  530.                 /* also possible instead of fh->fh_Type: Myprocess->pr_ConsoleTask */
  531.                 xioerr = IoErr();
  532.                 printf("ACS_2: DoPkt()=%ld, fh_Type=%ld, ConTask=%ld, oldport=%ld, IoErr()=%ld\n",xrslt,(long)fh->fh_Type,(long)Myprocess->pr_ConsoleTask,(long)acs_oldport,xioerr);
  533.             }
  534.             Close(bfh);
  535.         }
  536.         acs_kludge = 0;
  537.     }
  538. #endif
  539.  
  540.     if( !nowintitle && oldtitle )
  541.         SetWindowTitles(Mywindow,oldtitle,(char *)-1);
  542.     if( oldtaskpri != -999 )
  543.         SetTaskPri(&Myprocess->pr_Task,oldtaskpri);
  544.  
  545.     if (Mywindow)
  546.         Myprocess->pr_WindowPtr = old_WindowPtr;
  547.  
  548.     for (i=1; i<MAXMYFILES; i++) myclose(i);
  549.  
  550.     /* no effect if AnzMenus==0 (no menus in use) */
  551.     remove_menu();
  552.  
  553. #ifdef MULTIUSER_SUPPORT
  554.     /* LILJA: Added for multiuser-support */
  555.     if (muBase) CloseLibrary((struct Library *)muBase); muBase=NULL;
  556. #endif
  557.  
  558.     if (PatternBase) CloseLibrary( PatternBase );
  559.     CloseLibrary((struct Library *)GadToolsBase);
  560.     CloseLibrary((struct Library *)AslBase);
  561.     CloseLibrary((struct Library *)IntuitionBase);
  562.     CloseLibrary((struct Library *)GfxBase);
  563.     exit(n);
  564. }
  565.  
  566.  
  567. #if 1
  568. static void
  569. exectimer(int stop)
  570. {
  571.     struct DateStamp dss;
  572.     static struct DateStamp last;
  573.  
  574.     DateStamp(&dss);
  575.     if (stop) {
  576.         ExecTimer = dss.ds_Minute*6000 + 2*dss.ds_Tick;
  577.         ExecTimer += (dss.ds_Days-last.ds_Days)*6000*1440;
  578.         ExecTimer -= last.ds_Minute*6000 + 2*last.ds_Tick;
  579.     }
  580.     else
  581.         last = dss;
  582. }
  583. #else
  584. static void
  585. exectimer(int stop)
  586. {
  587.     struct DateStamp dss;
  588.     static long lasttime;
  589.     long time;
  590.  
  591.     DateStamp(&dss);
  592.     time=dss.ds_Minute*6000+2*dss.ds_Tick;   /* 2 = 100/TickPerSec   */
  593.     if( stop )
  594.         ExecTimer=time-lasttime;
  595.     else
  596.         lasttime=time;
  597. }
  598. #endif
  599.  
  600.  
  601. /* print break message only once */
  602. static BOOL dobreak_output = TRUE;
  603.  
  604. int
  605. breakcheck()
  606. {
  607.     return !o_nobreak && SetSignal(0L,0L) & SIGBREAKF_CTRL_C;
  608. }
  609.  
  610. void
  611. breakreset()
  612. {
  613.     /* We reset CTRL_E here for GMD's foreach() hack ... */
  614.     /* Why not just reset CTRL_F also -- for completeness? :) */
  615.  
  616.     SetSignal(0L, SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E);
  617.     dobreak_output = TRUE;
  618. }
  619.  
  620. dobreak()
  621. {
  622.     if (breakcheck()) {
  623.         if (dobreak_output) {
  624.             printf("^C\n");
  625.             dobreak_output = FALSE;
  626.         }
  627.         return(1);
  628.     }
  629.     return(0);
  630. }
  631.  
  632. /* this routine causes manx to use this Chk_Abort() rather than it's own */
  633. /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
  634. /* is zero).  Since we want to check for our own ^C's             */
  635.  
  636. long
  637. Chk_Abort()
  638. {
  639.     return(0);
  640. }
  641.  
  642. void
  643. _wb_parse()
  644. {
  645. }
  646.  
  647. do_howmany()
  648. {
  649.     char buf[10];
  650.  
  651.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  652.     GetVar(shellctr, buf, 10L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  653.     printf("Shell(s) running: %s\n",buf);
  654.     return 0;
  655. }
  656.  
  657. static void set_kickversion(void)
  658. {
  659.     struct Library *verlib;
  660.     static char verstr[10];
  661.     if (verlib = OpenLibrary("version.library",0)) {
  662.         sprintf(verstr,"%d",verlib->lib_Version);
  663.         CloseLibrary(verlib);
  664.     }
  665.     else
  666.         sprintf(verstr,"%d",SysBase->LibNode.lib_Version);
  667.     set_var(LEVEL_SET,"_kick",verstr);
  668. }
  669.  
  670. #if 1
  671. static struct Window *
  672. getwindow(void)
  673. {
  674.     struct Window *win = NULL;
  675.  
  676.     if( o_nowindow )
  677.         return NULL;
  678.  
  679.     if( IsInteractive(Output()) )
  680.         Write(Output(),"",1);     /*    make window appear */
  681.  
  682.     if( isconsole(Output()) && Output() ) {
  683.         if ( ((struct FileHandle *)BADDR(Output()))->fh_Type ) {
  684.             struct InfoData *infodata;
  685.             infodata=(void *)SAllocMem((long)sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC);
  686.             if (DoPkt((void *)((struct FileHandle *)BADDR(Output()))->fh_Type,ACTION_DISK_INFO,((LONG)infodata)>>2,NULL,NULL,NULL,NULL)) {
  687.                 win=(struct Window *)infodata->id_VolumeNode;
  688.             }
  689.             FreeMem(infodata,sizeof(struct InfoData));
  690.         }
  691.         if( win==NULL )
  692.             o_nowindow=1;
  693.     }
  694.  
  695.     newwidth();
  696.  
  697.     return win;
  698. }
  699. #else
  700. static struct Window *
  701. getwindow(void)
  702. {
  703.     struct Window *win = NULL;
  704.  
  705.     if( o_nowindow )
  706.         return NULL;
  707.     if( isconsole(Output()))
  708.         Write(Output(),"",1);     /*    make window appear */
  709.     if( Myprocess->pr_ConsoleTask ) {
  710.         struct InfoData *infodata;
  711.         infodata=(void *)SAllocMem((long)sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC);
  712.         if (DoPkt((void *)Myprocess->pr_ConsoleTask,ACTION_DISK_INFO,((LONG)infodata)>>2,NULL,NULL,NULL,NULL)) {
  713.             win=(struct Window *)infodata->id_VolumeNode;
  714.         }
  715.         FreeMem(infodata,sizeof(struct InfoData));
  716.         if( win==NULL )
  717.             o_nowindow=1;
  718.     }
  719.     newwidth();
  720.     return win;
  721. }
  722. #endif
  723.  
  724. #ifdef LATTICE
  725.  
  726. int
  727. setenv( char *var, char *val )
  728. {
  729.     char *buf=salloc(strlen(var)+strlen(val)+10);
  730.     sprintf(buf, "%s=%s", var, val );
  731.     Free(buf);
  732.     return putenv(buf);
  733. }
  734.  
  735. __regargs int __chkabort(void) { return(0); }
  736.  
  737. #endif
  738.  
  739. #ifdef _DCC
  740. void
  741. swapmem(void *_s1, void *_s2, size_t _n)
  742. {
  743.     char t, *s1=_s1, *s2=_s2;
  744.     int  i;
  745.  
  746.     for( i=0; i<_n; i++ )
  747.         t = *s1; *s1++ = *s2; *s2++=t;
  748. }
  749.  
  750. int
  751. setenv( char *var, char *val )
  752. {
  753.     BPTR fh;
  754.     char buf[40];
  755.     sprintf(buf,"ENV:%s",var );
  756.  
  757.     if( fh=Open(buf,MODE_NEWFILE) ) {
  758.         Write(fh,val,strlen(val));
  759.         Close(fh);
  760.     }
  761. }
  762.  
  763. int
  764. rand()
  765. {
  766.     return 0;
  767. }
  768.  
  769. _waitwbmsg() {return 0;};
  770.  
  771. #endif
  772.